home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / demos / OpenGL / space / watch.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  5KB  |  209 lines

  1. /*
  2.  * Copyright (C) 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include <sys/time.h>
  18. #include <GL/gl.h>
  19. #include "space.h" 
  20.  
  21. typedef struct  {
  22.         sint32 year ;
  23.         sint32 month ;
  24.         sint32 day ;
  25.         sint32 hour ;
  26.         sint32 min ;
  27.         flot64 sec ;
  28.         flot64 jd ;
  29. } t_time ;
  30.  
  31.        int            timer_flag = 0 ;
  32. static struct timeval bsdtime,lastime ;
  33. static flot64         epochdate = 2447891.5 ;
  34. static flot64         savet = 0 ;
  35. extern t_stopwatch Counter ;
  36.  
  37. static void   julian_date(t_time *) ;
  38.  
  39. /****************************************************************************
  40. *  check_timer()  - 
  41. ****************************************************************************/
  42. flot64 check_timer(void)
  43.  
  44. {  struct timezone tzp;
  45.    struct timeval  tp;
  46.    int             sec,usec;
  47.    t_time          tt ;
  48.    flot64          q ;
  49.    
  50.    if (!timer_flag)  {
  51.      timer_flag = 1 ;
  52.  
  53.      gettimeofday(&bsdtime, &tzp);
  54.      lastime = bsdtime ;
  55.  
  56.      tt.year  = 1970 ;
  57.      tt.month = 1 ;
  58.      tt.day   = 1 ;
  59.      tt.hour  = 0 ;
  60.      tt.min   = 0 ;
  61.      tt.sec   = 1.0e-6 * bsdtime.tv_usec;
  62.      tt.sec   += bsdtime.tv_sec ;
  63.      julian_date(&tt) ;
  64.  
  65.      Counter.D = tt.jd ;
  66.      printf("Julian Date: %f\n",Counter.D + epochdate) ;
  67.      }
  68.  
  69.    gettimeofday(&tp, &tzp);
  70.  
  71.    sec  = tp.tv_sec  - lastime.tv_sec;
  72.    usec = tp.tv_usec - lastime.tv_usec;
  73.    if (usec < 0) {
  74.       sec--;
  75.       usec += 1000000;
  76.       }
  77.    q  = 1.0e-6*usec ;
  78.    q += sec ;
  79.    
  80.    if (Counter.flags & TMREV_FLAG)
  81.      q = -q ;
  82.  
  83.    Counter.D += (Counter.timacc*q) / (24.0*3600.0) ;
  84.  
  85.    sec  = tp.tv_sec  - bsdtime.tv_sec;
  86.    usec = tp.tv_usec - bsdtime.tv_usec;
  87.    if (usec < 0) {
  88.       sec--;
  89.       usec += 1000000;
  90.       }
  91.    q  = 1.0e-6*usec ;
  92.    q += sec ;
  93.  
  94.    lastime = tp ;
  95.    return(q) ;
  96. }
  97.  
  98. /****************************************************************************
  99. *  delta_timer()  - 
  100. ****************************************************************************/
  101. flot64 delta_timer(void)
  102.  
  103. {  struct timezone tzp;
  104.    struct timeval  tp;
  105.    int             sec,usec;
  106.    flot64          q ;
  107.    
  108.    gettimeofday(&tp, &tzp);
  109.  
  110.    sec  = tp.tv_sec  - lastime.tv_sec;
  111.    usec = tp.tv_usec - lastime.tv_usec;
  112.    if (usec < 0) {
  113.       sec--;
  114.       usec += 1000000;
  115.       }
  116.    q  = 1.0e-6*usec ;
  117.    q += sec ;
  118.    
  119.    if (Counter.flags & TMREV_FLAG)
  120.      q = -q ;
  121.  
  122.    return((Counter.timacc*q) / (24.0*3600.0)) ;
  123. }
  124.  
  125. /****************************************************************************
  126. *  julian_date()  - 
  127. ****************************************************************************/
  128. static void julian_date(t_time *tt)
  129.  
  130. {  register sint32 B,C,D,julian,year,month ;
  131.  
  132.    year  = tt->year ;
  133.    month = tt->month ;
  134.  
  135.    if (year < 1582)
  136.      julian = 0 ;
  137.    else if (year > 1582)
  138.      julian = 1 ;
  139.    else if (month < 10)
  140.      julian = 0 ;
  141.    else if (month > 10)
  142.      julian = 1 ;
  143.    else if (tt->day < 15)
  144.      julian = 0 ;
  145.    else julian = 1 ;
  146.  
  147.    if (month == 1 || month == 2)  {
  148.      year-- ;
  149.      month+=12 ;
  150.      }
  151.  
  152.    if (julian)
  153.      B = 2 - (year/100) + (year/400) ;
  154.    else B = 0 ;
  155.  
  156.    if (year < 0)
  157.      C = (flot64) 365.25 * year - 0.75 ;
  158.    else C = (flot64) 365.25 * year ;
  159.  
  160.    D = (flot64) 30.6001*(month+1) ;
  161.  
  162.    tt->jd  = (flot64) B + C + D + tt->day + (1720994.5 - epochdate) +
  163.                         (tt->hour + tt->min/60.0 + tt->sec/3600.0)/24.0 ;
  164. }
  165.  
  166. /****************************************************************************
  167. *  reverse_julian_date()  - 
  168. ****************************************************************************/
  169. void reverse_julian_date(flot64 jd,char *date)
  170.  
  171. {  register flot64 q,f,dd ;
  172.    register sint32 i,b,a,c,d,e,g,mm,yy,day,hour,min ;
  173.  
  174.    q = jd + 0.5 + epochdate ;
  175.  
  176.    i = (int) q ;
  177.    f = q - i ;
  178.  
  179.    if (i > 2299160)  {
  180.      a = ((flot64)i - 1867216.25) / 36524.25 ;
  181.      b = i + 1 + a - (a>>2) ;
  182.      }
  183.    else b = i ;
  184.  
  185.    c = b + 1524 ;
  186.  
  187.    d = ((flot64)c - 122.1) / 365.25 ;
  188.  
  189.    e = 365.25 * (flot64)d ;
  190.  
  191.    g = ((flot64)c - (flot64)e) / 30.6001 ;
  192.  
  193.    dd = c - e + f - (sint32)(30.6001*(flot64)g) ;
  194.    dd -= 8.0/24.0 ;
  195.  
  196.    mm = ((g < 13.5) ? g-1 : g-13) ;
  197.  
  198.    yy = ((mm > 2.5) ? d-4716 : d-4715) ;
  199.  
  200.    day  = (sint32)dd ;
  201.    q    = 24.0*(dd-day) ;
  202.    hour = (sint32)q ;
  203.    q    = 60.0 * (q-hour) ;
  204.    min  = (sint32)q ;
  205.    
  206.    sprintf(date,"Date : %02d:%02d %02d/%02d %04d",hour,min,day,mm,yy) ;
  207. }
  208.  
  209.